package com.zx.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.zx.entity.Classes;
import com.zx.entity.Department;
import com.zx.service.ClassesService;
import com.zx.service.DepartmentService;
import com.zx.service.impl.ClassesServiceImpl;
import com.zx.service.impl.DepartmentServiceImpl;
import com.zx.util.JedisUtil;
import redis.clients.jedis.Jedis;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/ClassesServlet")
public class ClassesServlet extends BaseServlet {
    ClassesService classesService = new ClassesServiceImpl();
    DepartmentService departmentService = new DepartmentServiceImpl();
    public void select (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //接收要修改的id编号
        String did = request.getParameter("did");


        //判断缓存中这个系别的班级列表是否存在
        //Jedis jedis = new Jedis("localhost",6379);
        Jedis jedis = JedisUtil.getJedis();


        response.setContentType("text/html;charset=utf-8");
        PrintWriter writer = response.getWriter();

        String str = jedis.get("class"+did);
        if(str == null){
            System.out.println("缓存中没有，需要查询数据库");
            List<Classes> classesList = classesService.select(Integer.parseInt(did));

            ObjectMapper objectMapper = new ObjectMapper();
            str = objectMapper.writeValueAsString(classesList);

            jedis.set("class"+did,str);

        }else{
            System.out.println("缓存中有，直接获取");
        }
        writer.print(str);
        writer.close();
    }
}
